class Solution {
public:

    //https://gitee.com/link?target=https%3A%2F%2Fleetcode.cn%2Fproblems%2Ffruit-into-baskets%2F
    int totalFruit(vector<int>& fruits) 
    {
        int count = 0;
        vector<int> arr(fruits.size(), 0);
        int left = 0, right = 0;
        int k = 0;
        while(right < fruits.size())
        {
            if(arr[fruits[right]]++ == 0) k++;
            while(k > 2)
            {
                count = max(count, right - left);
                arr[fruits[left++]]--;
                if(arr[fruits[left - 1]] == 0)
                {
                    k--;
                }
            }
            right++;
        }
        if(k <= 2) count = max(count, right - left);
        return count;
    }

    int totalFruit1(vector<int>& fruits) 
    {
        int count = 0;
        vector<int> arr(fruits.size(), 0);
        int left = 0, right = 0;
        int k = 0;
        while(right < fruits.size())
        {
            if(arr[fruits[right]]++ == 0) k++;
            while(k > 2)
            {
                arr[fruits[left++]]--;
                if(arr[fruits[left - 1]] == 0)
                {
                    k--;
                }
            }
            count = max(count, right - left + 1);
            right++;
        }
        return count;
    }

    //https://gitee.com/link?target=https%3A%2F%2Fleetcode.cn%2Fproblems%2Ffind-all-anagrams-in-a-string%2F
    vector<int> findAnagrams(string s, string p) 
    {
        vector<int> v1;
        int hash1[26];
        int hash2[26];
        int count = 0;//这里需要用一个count来统计哈希中有效字符的个数，避免了哈希中的依次比较
        int k = p.size();
        cout<<"k:" << k << endl;
        for(auto& c:p)
        {
            hash2[c - 'a']++;
        }
        int left = 0, right = 0;
        while(right < s.size())
        {
            if(hash1[s[right] - 'a']++ < hash2[s[right] - 'a']) count++;
            if(right - left > k - 1)
            {
                if(hash1[s[left] - 'a']-- <= hash2[s[left] - 'a']) count--;//此处判断需注意<=
                left++;
            }
            if(count == k) v1.push_back(left);
            right++;
        }
        return v1;
    }
};